<template>
  <div class="AppointmentManage">
    <c-title :hide="false" :text="'项目管理'"></c-title>
    <van-search v-model="keyword" shape="round" placeholder="项目名称" @search="onsearch" />
    <van-tabs v-model="active" line-width="25px" @change="onchange">
      <van-tab :title="item.name" :name="item.id" v-for="(item, index) in classTab" :key="index"></van-tab>
    </van-tabs>
    <div class="listBox">
      <div class="lis" v-for="(item, index) in listData" :key="index">
        <div class="projectTitle">ID:{{ item.id }} {{ item.title }}</div>
        <div class="projectDetail">
          <div class="roll">
            <div class="num">{{ item.amount_count }}</div>
            <div class="txt">累计成交金额</div>
          </div>
          <div class="roll">
            <div class="num">{{ item.all_total_count }}</div>
            <div class="txt">累计成交次数</div>
          </div>
          <div class="roll">
            <div class="num">{{ item.used_count }}</div>
            <div class="txt">已完成次数</div>
          </div>
          <div class="roll">
            <div class="num">{{ item.booking_count }}</div>
            <div class="txt">预约中次数</div>
          </div>
          <div class="roll">
            <div class="num">{{ item.unused_count }}</div>
            <div class="txt">未预约次数</div>
          </div>
        </div>
        <div class="buttonBox">
          <div class="btn" @click="del(item.id)">删除</div>
          <div class="btn" style="margin-left: 0.75rem;" @click="edit(item.creator_id, item.id)">编辑</div>
        </div>
      </div>
      <van-empty image="search" description="暂无更多" v-if="listData.length <= 0" />
    </div>
    <div class="flexBox set-pc-styleLeft" @click="toAddProject">
      <div class="addPorject"><i class="iconfont icon-icon_add"></i>新增</div>
    </div>
  </div>
</template>
<script>
import { scrollMixin } from "utils/mixin";
export default {
  mixins: [scrollMixin], //加载更多
  data() {
    return {
      active: 0,
      keyword: "",
      classTab: [],
      listData: [],

      page: 1, //分页数，当前页数
      isLoadMore: true, //判断是否要加载更多的标志
      total_page: 0 //总页数
    };
  },
  activated() {
    this.init();
    this.keyword = "";
    this.active = "";
    this.listData = [];

    this.getClass();
    this.getData();
  },
  methods: {
    toAddProject() {
      this.$router.push(this.fun.getUrl("porjectEdit", {}));
    },
    edit(creator_id, id) {
      this.$router.push(this.fun.getUrl("porjectEdit", { category_id: 2, id: id }));
    },
    async del(id) {
      let { data, msg, result } = await $http.post("plugin.appointment.frontend.project.store-project-delete", { id: id }, "");
      if (!result) return this.$toast(msg);
      this.$toast(msg);
      this.init();
      this.getData();
    },
    async getData() {
      let json = {
        search: {}
      };
      json.search.category_id = this.active ? this.active : "";
      json.search.title = this.keyword;
      let { data, msg, result } = await $http.post("plugin.appointment.frontend.project.store-project-list", json, "");
      if (result) {
        this.listData = data.data;
        this.isLoadMore = true;
        this.total_page = data.last_page;
        if (!this.total_page) {
          this.total_page = 0;
        }
      } else {
        this.$toast(msg);
      }
    },
    init() {
      this.isLoadMore = true;
      this.total_page = 0;
      this.page = 1;
    },
    onsearch() {
      this.init();
      this.getData();
    },
    onchange() {
      this.init();
      this.getData();
    },
    async getClass() {
      let { data, msg, result } = await $http.post("plugin.appointment.frontend.project-category.get-list", {}, "");
      this.classTab = data;
      let all = {
        id:'',
        name:'全部'
      }
      this.classTab.unshift(all)
    },
    async getMoreData() {
      this.isLoadMore = false;
      if (this.page >= this.total_page) {
        return;
      } else {
        this.page = this.page + 1;
        let json = {
          search: {},
          page: this.page
        };
        json.search.category_id = this.active ? this.active : "";
        json.search.title = this.keyword;
        let { data, msg, result } = await $http.post("plugin.appointment.frontend.project.store-project-list", json, "");
        this.isLoadMore = true;
        if (result) {
          var nextData = data.data;
          this.listData = this.listData.concat(nextData);
        } else {
          this.page = this.page - 1;
          this.isLoadMore = false;
        }
      }
    }
  }
};
</script>
<style lang="scss" scoped>
.AppointmentManage {
  overflow: hidden;
  .flexBox {
    position: fixed;
    right: 0.75rem;
    bottom: 6.88rem;
  }
  .addPorject {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    font-size: 0.63rem;
    color: #ffffff;
    background: #f15353;
    border-radius: 50%;
    width: 2.75rem;
    height: 2.75rem;
  }
  .listBox {
    margin: 0.63rem 0.75rem;
    .lis {
      background: #ffffff;
      border-radius: 0.5rem;
      padding: 0.88rem 0.75rem 0.75rem 0.75rem;
      margin-bottom: 0.63rem;
      .buttonBox {
        display: flex;
        justify-content: flex-end;
        .btn {
          padding: 0.59rem 1.31rem;
          font-size: 0.81rem;
          border-radius: 1.88rem;
          border: 0.03rem solid #aaaab3;
        }
      }
      .projectDetail {
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        margin-bottom: 0.75rem;
        .roll {
          width: 33.3%;
          display: flex;
          flex-direction: column;
          justify-content: flex-start;
          align-items: center;
          margin-bottom: 0.94rem;
        }
      }
      .projectTitle {
        font-weight: bold;
        font-size: 0.88rem;
        line-height: 1.13rem;
        text-align: left;
        margin-bottom: 0.75rem;
      }
    }
  }
}
</style>
